package com.xea.service;

import java.util.List;

import com.uf.util.PageData;
import com.uf.util.SqlHelper;
import com.xea.model.News;
public class NewsService {
	
	/**
	 * 查询新闻
	 * @param name       标题名称
	 * @param topage     第几页
	 * @return
	 */
	public PageData search(String name,String topage){
		
		String sql = "select * from News where title like '%"+name+"%' order by id";
		return SqlHelper.queryPageForMysql(sql, null, topage, null, News.class);	
		
	}
	
	/**
	 * 查询新闻
	 * @param name       标题名称
	 * @param topage     第几页
	 * @return
	 */
	public PageData searchforout(String topage){
		
		String sql = "select * from News where pub=1 order by id";
		return SqlHelper.queryPageForMysql(sql, null, topage, "10", News.class);	
		
	}
	
	/**
	 * 查询新闻公开
	 * @return
	 */
	public List<News> searchTop(){
		
		String sql = "select a.* from (select * from News where pub=1 order by createDate desc)a limit 0,5";
		return SqlHelper.query(sql, null, News.class);
		
	}
	
	/**
	 * 新增新闻
	 * @param news   新闻实体
	 * @return
	 */
	public void add(News news){
		
		String sql = "insert into News(title,content,createDate,pub) values(?,?,?,?)";
		Object[] params=new Object[]{news.getTitle(),news.getContent(),news.getCreateDate(),news.getPub()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 新闻详情
	 * @param id  id 
	 * @return
	 */
	public News findById(int id){
		
		String sql = "select * from News where id = ?";		
		List<News> list = SqlHelper.query(sql, new Object[]{ id}, News.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 删除新闻
	 * @param id   id
	 * @return
	 */
	public void del(String id){
		
		String sql = "delete from News where id in("+id+")";
		SqlHelper.excute(sql, null);
		
	}
	
	/**
	 * 修改是否公开
	 * @param id   id
	 * @param pub   是否公开
	 * @return
	 */
	public void pub(String id,int pub){
		
		String sql = "update News set pub=? where id in("+id+")";
		Object[] params=new Object[]{pub};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 修改新闻
	 * @param product   新闻实体
	 * @return
	 */
	public void update(News news){
		
		String sql = "update News set title=?,content=?,pub=? where id=?";
		Object[] params=new Object[]{news.getTitle(),news.getContent(),news.getPub(),news.getId()};
		SqlHelper.excute(sql, params);
		
	}
}
